
package gui.panels;

import coffee.Constants;
import coffee.Statistics;
import gui.MainFrame;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 *
 * @author JtR
 */
public class StatisticsContentPanel extends JPanel {
    private JLabel name, weight, office, fav1, fav2;
    private JLabel drinkName, price, caffein, date;
    
    public StatisticsContentPanel() {
        initComponents();
        updateFields();
    }
    
    private void initComponents() {
        setLayout(new GridBagLayout());
        setBackground(Color.WHITE);
        
        JPanel leftPanel = new JPanel(new GridBagLayout());
        leftPanel.setBackground(Color.WHITE);
        leftPanel.setBorder(new LineBorder(Color.BLACK));
        add(leftPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(20, 20, 20, 20), 0, 0));
        
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.NORTHWEST;
        
        Insets i00 = new Insets(20, 20, 0, 20);
        Insets i0 = new Insets(0, 20, 0, 20);
        Insets i1 = new Insets(0, 50, 0, 20);
        Insets i2 = new Insets(0, 50, 20, 20);
        
        
        JLabel t1 = new JLabel("Daten");
        JLabel t2 = new JLabel("Name:");
        name = new JLabel();
        JLabel t5 = new JLabel("Büro:");
        office = new JLabel();
        JLabel t3 = new JLabel("Gewicht:");
        weight = new JLabel();
        JLabel t4 = new JLabel("Standardgetränke:");
        fav1 = new JLabel();
        fav2 = new JLabel();
        
        Font b = t1.getFont().deriveFont(18f);
        Font f = b.deriveFont(Font.PLAIN);
        t1.setFont(f);
        t2.setFont(f);
        name.setFont(b);
        t5.setFont(f);
        office.setFont(b);
        t3.setFont(f);
        weight.setFont(b);
        t4.setFont(f);
        fav1.setFont(b);
        fav2.setFont(b);
        
        c.insets = i00;
        leftPanel.add(t1, c);
        c.insets = i0;
        leftPanel.add(t2, c);
        c.insets = i1;
        leftPanel.add(name, c);
        c.insets = i0;
        leftPanel.add(t5, c);
        c.insets = i1;
        leftPanel.add(office, c);
        c.insets = i0;
        leftPanel.add(t3, c);
        c.insets = i1;
        leftPanel.add(weight, c);
        c.insets = i0;
        leftPanel.add(t4, c);
        c.insets = i1;
        leftPanel.add(fav1, c);
        c.insets = i2;
        leftPanel.add(fav2, c);
        
        JPanel statisticsWrapper = new JPanel(new GridBagLayout());
        statisticsWrapper.setBackground(Color.WHITE);
        statisticsWrapper.setBorder(new LineBorder(Color.BLACK));
        c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTHEAST;
        c.insets = new Insets(20, 0, 0, 20);
        add(statisticsWrapper, c);
        
        c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(20, 20, 20, 0);
        drinkName = new JLabel();
        drinkName.setFont(drinkName.getFont().deriveFont(Font.PLAIN));
        statisticsWrapper.add(drinkName, c);
        
        price = new JLabel();
        price.setFont(price.getFont().deriveFont(Font.PLAIN));
        statisticsWrapper.add(price, c);
        
        caffein = new JLabel();
        caffein.setFont(caffein.getFont().deriveFont(Font.PLAIN));
        statisticsWrapper.add(caffein, c);
        
        date = new JLabel();
        date.setFont(date.getFont().deriveFont(Font.PLAIN));
        c.insets = new Insets(20, 20, 20, 20);
        statisticsWrapper.add(date, c);
    }
    
    public void updateFields() {
        DateFormat formatter = new SimpleDateFormat("HH:mm   dd.MM.yy");
        name.setText(MainFrame.user.getName());
        office.setText(MainFrame.user.getRoom());
        weight.setText(MainFrame.user.getWeight() + " kg"); 
        fav1.setText(MainFrame.user.getFavouriteCoffee() != null ? MainFrame.user.getFavouriteCoffee().toString() : "Nicht festegelegt");
        fav2.setText(MainFrame.user.getFavouriteTea() != null ? MainFrame.user.getFavouriteTea().toString() : "Nicht festegelegt");
        
        StringBuilder drinkNameStr = new StringBuilder("<html><b>Getränk:</b>");
        StringBuilder priceStr = new StringBuilder("<html><b>Preis:</b>");
        StringBuilder caffeinStr = new StringBuilder("<html><b>Koffein (mg):</b>");
        StringBuilder dateStr = new StringBuilder("<html><b>Uhrzeit/Datum:</b>");
        
        Collections.reverse(MainFrame.user.getStatistic());
        int count = 25;
        for(Statistics s : MainFrame.user.getStatistic()) {
            if(count-- == 0)
                break;
            switch(s.getDrink()) {
                case Constants.BLACK_TEA:
                    drinkNameStr.append("<br>Schwarzer Tee");
                    break;
                case Constants.FRUIT_TEA:
                    drinkNameStr.append("<br>Früchtetee");
                    break;
                case Constants.GREEN_TEA:
                    drinkNameStr.append("<br>Grüner Tee");
                    break;
                case Constants.COFFEE:
                    drinkNameStr.append("<br>Kaffee");
                    break;
                case Constants.CAPPUCCINO:
                    drinkNameStr.append("<br>Cappuccino");
                    break;
                case Constants.ESPRESSO:
                    drinkNameStr.append("<br>Espresso");
                    break;
            }
            priceStr.append("<br>");
            priceStr.append(s.getPriceAsString());
            priceStr.append(" €");
            caffeinStr.append("<br>");
            caffeinStr.append(s.getCaffein());
            dateStr.append("<br>");
            dateStr.append(formatter.format(new Date(s.getTimestamp())));
        }
        Collections.reverse(MainFrame.user.getStatistic());
        
        drinkNameStr.append("</html>");
        priceStr.append("</html>");
        caffeinStr.append("</html>");
        dateStr.append("</html>");
        
        drinkName.setText(drinkNameStr.toString());
        price.setText(priceStr.toString());
        caffein.setText(caffeinStr.toString());
        date.setText(dateStr.toString());
    }
}
